﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;

namespace Marmat.Forms
{
    [ToolboxItem(true)]
    [Designer("Marmat.Forms.BToolStripDesigner")]
    public partial class ToolStrip : System.Windows.Forms.UserControl, IControl
    {
        public ToolStrip()
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this.SetStyle(ControlStyles.ResizeRedraw, true); // 尺寸变化后绘制
            this.SetStyle(ControlStyles.Opaque, false); // 改善绘图质量
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); // 使控件支持透明色
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.Dock = DockStyle.Top;
        }

        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        [Category("数据")]
        [Description("返回 Table 的列集合")]
        public virtual BarItemCollection Items
        {
            get
            {
                if (this._Items is null)
                {
                    this._Items = new BarItemCollection(this.BarManager1, this.Bar1.LinksPersistInfo, null);
                }

                return this._Items;
            }
        }

        private BarItemCollection _Items;

        [DefaultValue(typeof(DockStyle), "Top")]
        public override DockStyle Dock
        {
            get => base.Dock;

            set => base.Dock = value;
        }

        [DefaultValue(-1)]
        public int BarItemHorzIndent
        {
            get
            {
                return this.Bar1.BarItemHorzIndent;
            }

            set
            {
                this.Bar1.BarItemHorzIndent = value;
            }
        }

        [DefaultValue(-1)]
        public int BarItemVertIndent
        {
            get
            {
                return this.Bar1.BarItemVertIndent;
            }

            set
            {
                this.Bar1.BarItemVertIndent = value;
            }
        }

        private void BarManager1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.FindForm() is object)
                this.FindForm().Validate();
            var item = this.Find(this.Items, e);
            if (item is object)
            {
                var EventArgs = new ToolStripItemClickedEventArgs(item);
                EventArgs.Item = item;
                ItemClicked?.Invoke(this, EventArgs);
            }
        }

        private BarItem Find(BarItemCollection Collection, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            BarItem result = null;
            foreach (BarItem item in Collection)
            {
                if (ReferenceEquals(item.barItem, e.Item))
                {
                    result = item;
                    return result;
                }

                if (item is BarSubItem)
                {
                    result = this.Find(((BarSubItem)item).Items, e);
                    if (result is object)
                    {
                        return result;
                    }
                }
            }

            return result;
        }

        private void ContextMenuStrip_Disposed(object sender, EventArgs e)
        {
            this.Item_Disposed(this.Items);
        }

        private void Item_Disposed(BarItemCollection Collection)
        {
            foreach (BarItem item in Collection)
            {
                if (item is BarSubItem)
                {
                    this.Item_Disposed(((BarSubItem)item).Items);
                }
            }
        }

        public event ItemClickedEventHandler ItemClicked;

        public delegate void ItemClickedEventHandler(object sender, ToolStripItemClickedEventArgs e);

        internal void RaiseEventPopup(BarSubItem Item)
        {
            Popup?.Invoke(this, Item);
        }

        public event PopupEventHandler Popup;

        public delegate void PopupEventHandler(object sender, BarSubItem e);
    }
}